<?php
include_once 'Loader.class.php';
include_once 'DbError.class.php';
include_once 'app/until/GFW_database.php';
include_once 'Db_Core.class.php';
include_once 'app/until/get.php';
include_once 'app/until/post.php';
include_once 'app/until/file.php';
include_once 'app/until/GetData.php';
include_once 'app/until/HTTP.php';
include_once 'libs/templateEngine/rain.tpl.class.php';
include_once 'libs/ip2locationlite.class.php';
include_once 'libs/ThumbMaker/ThumbLib.inc.php';

/**
 * @author gregor
 *
 *
 */

class Gregor_Main_Launcher {
	//TODO - Insert your code here

	private $basedir;
	public $Settings;
	protected $SettingsDir;
	private $getUrl;
	private $found = array();
	public $url ;
	private $element;
	private $founfUrl = false;
	private $var = array();
	public $Post;
	public $Get;
	public $File;
	public $request;
	public $design;

	public $ln = 'de';


	function __construct($SettingDir = '') {
	    
		/******
		 * Url catch und s�ubern
		 */
		$this->fillUrl($_SERVER['REQUEST_URI']);
			
		/******
		 * Load Settings
		 */
		$this->SettingsDir = $SettingDir;
		$this->LoadSettings();

		$this->basedir =  '\\'.$this->Settings['BASE_DIR'];
		$this->url = $this->UrlCleaner();
			
		/******
		 * Url from Settings Load
		 */
		$this->getUrlFromSetting();
		
		/******
		 * Template Aufrufen
		 */
		$this->LoadLanguage();
		
		/******
		 * Template Aufrufen
		 */
		$this->LoadDesign();
			
			
			
		/******
		 * Controller Loader
		 */
		$this->ControllerLoader();
	}

	/******
	 * Function um die ur zu s�ubern
	 */
    function fillUrl($url){
	    if(isset($_GET['que'])){
	        $this->url = $_GET['que'];
	    }else{
		    $this->url = $url;
	    }
    }
    
	function LoadSettings(){
	    $this->Settings = include $this->SettingsDir.'setting.php';
	    $ServerName = str_replace("www.", "", $_SERVER['HTTP_HOST']);
	    if (is_file($this->SettingsDir.$ServerName.'.php')) {
	    	$tmps = include $this->SettingsDir.$ServerName.'.php';
	    	foreach ($tmps as $key => $live){
	    		if(array_key_exists($key, $this->Settings)){
	    			$this->Settings[$key] = $live; 
	    		}
	    	}
	    }
	    define('BASE_URL',$this->Settings['BASE_DIR']);
	    
	    include_once 'conf/Konstanten.php';
	}
	
	function LoadLanguage(){	    
		if(isset($_COOKIE['GFW']['ln'])){
			$this->ln = $_COOKIE['GFW']['ln'];
		}elseif(isset($this->Get->ln)){
			$this->ln = $this->Get->ln;
			setcookie('GFW[ln]',$this->ln);
		}else{
			setcookie('GFW[ln]',$this->ln);
		}
	}

	/******
	 * Function um die ur zu s�ubern
	 */
	function LoadDesign(){
	    
    	raintpl::configure("base_url", $this->Settings['BASE_DIR'] );
    	raintpl::configure("tpl_ext", "tpl" );    	
    	
	
		$this->design = new RainTPL($this->Settings['BASE_DIR']);

        $this->design->configure( 'tpl_dir', 'templates/' );
		$this->design->assign('userimageurl',$this->Settings['web_image']);		
		$this->design->assign('baseurl',$this->Settings['BASE_DIR']);	
		$this->design->assign('title',$this->Settings['Default_Titel']);		
		$this->design->url = $this->url;			
		$this->design->ln = $this->ln;
		
		$this->design->post    =  new Post();
		$this->design->get     =  new Get();
		$this->design->file    =  new File();
	}

	/******
	 * Function um die ur zu s�ubern
	 */
	function UrlCleaner(){
		$tmp_var = explode('?',$this->url);
		$this->url = $tmp_var[0];
		
		if(strlen($this->basedir) > 1){
			return preg_replace('/^'.$this->basedir.'/','',$this->url);
		}else{
			return $this->url;
		}
	}

	/******
	 * Function die Url und controller raushollt
	 */
	function getUrlFromSetting(){
		$this->getUrl = explode('%',$this->Settings['URL']);
	}

	/******
	 * Saubern von den Url variablen
	 */
	function CleanVariableName($var = NULL){
	    //TODO - Das geht auch Besser mit replace
		$var = substr($var, 0, -1);
		$var = substr($var, 1);

		return $var;
	}

	function CreatMatchVariable($Varnamen,$Value){
		return $this->element[$Varnamen] = $Value;
	}
	function GetMatchVariableName($key){
		preg_match_all('(#\w+#)',$this->getUrl[$key],$this->var);
	}
	function CleanSettingUrl($key){
		$this->getUrl[$key] =	preg_replace('(#\w+#)','',$this->getUrl[$key]);
		$this->getUrl[$key] =	preg_replace('([\/])','\/',$this->getUrl[$key]);
	}
	function CheckIfUrlGet($key){
		return preg_match("/^".$this->getUrl[$key]."/i",$this->url);
	}
	function getFoundeVariable($key){
	   preg_match_all("/^".$this->getUrl[$key]."/is",$this->url,$this->found);
	}
	public function ControllerLoader(){
		for ($i = 1;$i < (sizeof($this->getUrl));$i = $i+2)
		{
			$this->GetMatchVariableName($i);
			$this->CleanSettingUrl($i);
				
			if (@$this->CheckIfUrlGet($i))
			{
				$this->getFoundeVariable($i);
				for ($x =0;$x<sizeof($this->var[0]);$x++)
				{
					$this->var[0][$x] = $this->CleanVariableName($this->var[0][$x]);
					$this->CreatMatchVariable($this->var[0][$x],$this->found[$x+1][0]);
				}
				$this->founfUrl = true;
				$controller = new ControllerLoader($this->design,$this->getUrl[$i+1],$this->element);

				if ($controller->WhatToDo)
				{
					break;
				}
			}
		}
		if ($this->founfUrl == false)
		{
			//HTTP::redirect($this->Settings['404']);
		}

	}
}

?>